package com.ruoyi.crm.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.crm.domain.ProductInfo;
import com.ruoyi.crm.service.IProductInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 *
 * 产品管理控制层
 * @author  dxy
 *
 */

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/product")
public class ProductInfoController extends BaseController {
    private final IProductInfoService productInfoService;

    /**
     * 分页查询产品管理列表
     *
     * @param productInfo
     * @param pageQuery
     * @return
     */

    @SaCheckPermission("product:productInfo:list")
    @GetMapping("/list")
    public TableDataInfo<ProductInfo> list(ProductInfo productInfo, PageQuery pageQuery) {
        TableDataInfo<ProductInfo> list = productInfoService.selectProductPage(productInfo, pageQuery);
        return list;
    }

    /**
     * 新增产品管理
     */
    @SaCheckPermission("product:productInfo:add")
    @Log(title = "产品管理列表", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody ProductInfo productInfo) {
        return toAjax(productInfoService.insertProductById(productInfo));
    }

    /**
     * 修改产品管理
     */
    @SaCheckPermission("product:productInfo:edit")
    @Log(title = "产品管理列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody ProductInfo productInfo) {
        return toAjax(productInfoService.updateProductById(productInfo));
    }
    /**
     * 删除产品管理
     */
    @SaCheckPermission("product:productInfo:remove")
    @Log(title = "产品管理列表",businessType = BusinessType.DELETE)
    @DeleteMapping("/{productId}")
    public R<Void> remove(@PathVariable Long productId) {
        return toAjax(productInfoService.delectProductById(productId));
    }

    /**
     * 导出产品管理
     */
    @SaCheckPermission("product:productInfo:export")
    @Log(title = "产品管理列表",businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductInfo productInfo) {
        List<ProductInfo> list=productInfoService.selectProductList(productInfo);
        List<ProductInfo> listVo= BeanUtil.copyToList(list,ProductInfo.class);
        ExcelUtil.exportExcel(listVo, "产品管理数据", ProductInfo.class, response);
    }
    /**
     * 查询产品列表
     */
    @GetMapping("/product")
    public R<List<ProductInfo>> getProductList() {
        List<ProductInfo> list=productInfoService.selectProductList(new ProductInfo());
        return R.ok(list);
    }

    /**
     * 批量新增产品列表
     */
    @PostMapping("/products")
    @Log(title = "产品管理列表", businessType = BusinessType.INSERT)
    public R<Void> save(@Validated @RequestBody List<ProductInfo> list) {
        return toAjax(productInfoService.insertProductByIds(list));
    }

}
